/**
 * Created by yangxianming on 2018/7/5.
 */
//
//接口定义 都是以api开头 所需参数都通过方法传入
//
function apiGetCorpList(dofun, envText){
    var data = {
        "action": "getCorpList",
        "env": envText
    };

    var xmlHttp1 = new XMLHttpRequest();
    xmlHttp1.onreadystatechange = function(){
        if (xmlHttp1.readyState==4){
            if(xmlHttp1.status==200){
                var r = JSON.parse(xmlHttp1.response);
                if (r["apiStatus"] == "1"){
                    dofun(r["result"]);
                }else{
                    alert(r["errorMsg"]);
                }
            }
        }
    };
    xmlHttp1.open("POST", "/efuli/corpMng", true);
    xmlHttp1.send(JSON.stringify(data));
}


//
//页面渲染 都是以draw开头
//
function drawGetCorpList(data){
    var tableInfo = document.getElementById("cal").getElementsByTagName("tbody")[0];
    //清空所有行
    var firstTr = tableInfo.getElementsByTagName("tr")[0];
    tableInfo.innerText = "";
    tableInfo.appendChild(firstTr);

    var list = data;
    for (var i = 0; i < list.length; i++){
        var tr = document.createElement("tr");
        var myInput = document.createElement("input");
        var mySpan = document.createElement("span");
        tr.setAttribute("corpId", list[i].corpId);
        tr.onmouseover = function(){
            if (this.getAttribute("id") != "highLight"){
                this.setAttribute("style", "background-color: #ffeaea");
            }
        };
        tr.onmouseout = function(){
            if (this.getAttribute("id") != "highLight"){
                this.setAttribute("style", this.getAttribute("originalColor"));
            }
        };

        //企业名称
        var td = document.createElement("td");
        td.innerText = list[i].companyName;
        tr.appendChild(td);

        //企业id
        td = document.createElement("td");
        td.innerText = list[i].corpId;
        tr.appendChild(td);

        //企业员工数
        td = document.createElement("td");
        td.innerText = list[i].coreUserCount;
        tr.appendChild(td);

        //对应折扣商家数
        td = document.createElement("td");
        td.innerText = list[i].compantMerchantDiscount;
        tr.appendChild(td);

        //是否开通福利金买单
        td = document.createElement("td");
        myInput = document.createElement("input");
        var is_open_flpay = list[i].is_open_flpay;
        myInput.setAttribute("class", "checkboxForTD");
        myInput.setAttribute("type", "checkbox");
        if(is_open_flpay == "Y"){
            myInput.setAttribute("checked", "checked");
        }
        myInput.onclick = function(){switchFLpay(this, this.parentNode.parentNode.getAttribute("corpId"))};
        td.appendChild(myInput);
        tr.appendChild(td);

        //是否开通转赠
        td = document.createElement("td");
        myInput = document.createElement("input");
        var is_open_gifting = list[i].gifting_status;
        myInput.setAttribute("class", "checkboxForTD");
        myInput.setAttribute("type", "checkbox");
        if(is_open_gifting == "Y"){
            myInput.setAttribute("checked", "checked");
        }
        myInput.onclick = function(){switchGifting(this, this.parentNode.parentNode.getAttribute("corpId"))};
        td.appendChild(myInput);
        tr.appendChild(td);

        //是否开通代扣
        td = document.createElement("td");
        myInput = document.createElement("input");
        var withhold_status = list[i].withhold_status;
        myInput.setAttribute("class", "checkboxForTD");
        myInput.setAttribute("type", "checkbox");
        if(withhold_status == "Y"){
            myInput.setAttribute("checked", "checked");
        }
        myInput.onclick = function(){switchWithhold(this, this.parentNode.parentNode.getAttribute("corpId"))};
        td.appendChild(myInput);
        tr.appendChild(td);

        //代扣方式
        td = document.createElement("td");
        var form1 = document.createElement("form");
        var myInputRadio1 = document.createElement("input");
        var myInputRadio2 = document.createElement("input");
        var withhold_model = list[i].withhold_model;
        myInputRadio1.setAttribute("type", "radio");
        myInputRadio1.setAttribute("class", "radioForTD");
        myInputRadio1.setAttribute("name", "send");
        myInputRadio1.setAttribute("value", "1");
        myInputRadio1.onclick = function(){switchwithhold_model("1",this.parentNode.parentNode.parentNode.getAttribute("corpId"))};
        myInputRadio1.style.width = "40px";
        if(withhold_model == "1"){
            myInputRadio1.setAttribute("checked", "checked");
        }
        myInputRadio2.setAttribute("type", "radio");
        myInputRadio2.setAttribute("class", "radioForTD");
        myInputRadio2.setAttribute("name", "send");
        myInputRadio2.setAttribute("value", "2");
        myInputRadio2.onclick = function(){switchwithhold_model("2",this.parentNode.parentNode.parentNode.getAttribute("corpId"))};
        if(withhold_model == "2"){
            myInputRadio2.setAttribute("checked", "checked");
        }
        if(withhold_status != "Y"){
            myInputRadio1.setAttribute("disabled", "disabled");
            myInputRadio2.setAttribute("disabled", "disabled");
        }
        form1.appendChild(myInputRadio1);
        form1.appendChild(myInputRadio2);
        td.appendChild(form1);
        tr.appendChild(td);

        //1福利金=X人名币
        td = document.createElement("td");
        //td.innerText = list[i].rmb_point_ratio;
        //tr.appendChild(td);
        myInput = document.createElement("input");
        myInput.setAttribute("class", "forTD");
        myInput.style.backgroundColor = "transparent";
        myInput.placeholder = "输入金额";
        myInput.value = list[i].rmb_point_ratio;
        myInput.style.width = "40%";

        mySpan = document.createElement("span");
        mySpan.innerText = "设置";
        mySpan.style.width = "60%";
        mySpan.style.cursor = "pointer";
        mySpan.onclick = function(){
            updateRmbPointRatio(
                this.parentNode.getElementsByTagName("input")[0].value.trim(),
                this.parentNode.parentNode.getAttribute("corpId"))
        };
        td.appendChild(myInput);
        td.appendChild(mySpan);
        tr.appendChild(td);


        //企业余额
        td = document.createElement("td");
        td.innerText = list[i].account_balance;
        tr.appendChild(td);

        //企业充值
        td = document.createElement("td");
        myInput = document.createElement("input");
        myInput.setAttribute("class", "forTD");
        myInput.style.backgroundColor = "transparent";
        myInput.placeholder = "输入金额";
        myInput.style.width = "45%";
        mySpan = document.createElement("span");
        mySpan.innerText = "充";
        mySpan.style.width = "30%";
        mySpan.style.cursor = "pointer";
        mySpan.onclick = function(){corpRecharge(this.parentNode.parentNode.getAttribute("corpId"), this.parentNode)};
        td.appendChild(myInput);
        td.appendChild(mySpan);
        tr.appendChild(td);

        //操作按钮
        td = document.createElement("td");
        var div = document.createElement("div");
        div.innerText = "添加员工";
        div.style.cursor = "pointer";
        div.style.marginRight = "10px";
        div.onclick = function(){emp(this.parentNode.parentNode.getAttribute("corpId"))};
        td.appendChild(div);
        tr.appendChild(td);

        div = document.createElement("div");
        div.innerText = "员工列表";
        div.style.cursor = "pointer";
        div.onclick = function(){
            document.getElementById("realName").value = "";
            empListTable(this.parentNode.parentNode.getAttribute("corpId"), "1")};
        td.appendChild(div);
        tr.appendChild(td);

        tableInfo.appendChild(tr);
    }
}


//
//页面调用 数据获取，接口层与渲染层的连接
//
function initializeData(){
    //获取页面数据
    var env = document.getElementById("env");
    var envText = env.options[env.selectedIndex].value;
    //调接口
    apiGetCorpList(drawGetCorpList, envText);
}

//——————————旧代码--------------


//代扣方式 发放扣，消费扣 提交
function switchwithhold_model(value, corpId){
    var env = document.getElementById("env");
    var envText = env.options[env.selectedIndex].value;
    var data = {
        "action":"withhold_model",
        "env": envText,
        "corp_id": corpId,
        "operate": value
    };
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open("POST", "/efuli/corpMng", true);
    xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState==4){
            if (xmlhttp.status==200){
                var r = JSON.parse(xmlhttp.response);
                if (r["apiStatus"] == "1"){
                    //没想好做啥
                }else{
                    alert(r["errorMsg"]);
                }
            }else{
                alert("服务器异常");
            }
        }
    };
    xmlhttp.send(JSON.stringify(data));
}

//是否开通转赠开关
function switchGifting(inputOb, corpId){
    var env = document.getElementById("env");
    var envText = env.options[env.selectedIndex].value;
    var data = {
        "action":"isOpenGifting",
        "env": envText,
        "corp_id": corpId
    };
    //获取发放扣和消费口输入对象
    if(inputOb.checked){
        data["operate"] = "Y";
    }else{
        data["operate"] = "N";
    }
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open("POST", "/efuli/corpMng", true);
    xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState==4){
            if (xmlhttp.status==200){
                var r = JSON.parse(xmlhttp.response);
                if (r["apiStatus"] == "1"){

                }else{
                    alert(r["errorMsg"]);
                }

            }else{
                alert("服务器异常");
            }
        }
    };
    xmlhttp.send(JSON.stringify(data));
}

//是否开通代扣开关
function switchWithhold(inputOb, corpId){
    var env = document.getElementById("env");
    var envText = env.options[env.selectedIndex].value;
    var data = {
        "action":"isOpenWithhold",
        "env": envText,
        "corp_id": corpId
    };
    //获取发放扣和消费口输入对象
    var form = inputOb.parentNode.parentNode.getElementsByTagName("td")[7].getElementsByTagName("form")[0];
    var input3 = form.getElementsByTagName("input")[0];
    var input4 = form.getElementsByTagName("input")[1];
    if(inputOb.checked){
        input3.removeAttribute("disabled");
        input4.removeAttribute("disabled");
        data["operate"] = "Y";
    }else{
        input3.setAttribute("disabled", "disabled");
        input4.setAttribute("disabled", "disabled");
        data["operate"] = "N";
    }
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open("POST", "/efuli/corpMng", true);
    xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState==4){
            if (xmlhttp.status==200){
                var r = JSON.parse(xmlhttp.response);
                if (r["apiStatus"] == "1"){

                }else{
                    alert(r["errorMsg"]);
                }
            }else{
                alert("服务器异常");
            }
        }
    };
    xmlhttp.send(JSON.stringify(data));
}

//福利金买单开关
function switchFLpay(inputOb, corpId){
    var env = document.getElementById("env");
    var envText = env.options[env.selectedIndex].value;
    var data = {
        "action":"isOpenFLpay",
        "env": envText,
        "corp_id": corpId
    };
    if(inputOb.checked){
        data["operate"] = "Y";
    }else{
        data["operate"] = "N";
    }

    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open("POST", "/efuli/corpMng", true);
    xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState==4){
            if (xmlhttp.status==200){
                var r = JSON.parse(xmlhttp.response);
                if (r["apiStatus"] == "1"){

                }else{
                    alert(r["errorMsg"]);
                }

            }else{
                alert("服务器异常");
            }
        }
    };
    xmlhttp.send(JSON.stringify(data));
}

//设置福利金与rmb的兑换比率 （设置rmb_point_ratio）
function updateRmbPointRatio(inputValue, corpId){
    var env = document.getElementById("env");
    var envText = env.options[env.selectedIndex].value;
    var data = {
        "action":"setRmbpointratio",
        "env": envText,
        "corp_id": corpId,
        "rmb_point_ratio": inputValue
    };


    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open("POST", "/efuli/corpMng", true);
    xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState==4){
            if (xmlhttp.status==200){
                var r = JSON.parse(xmlhttp.response);
                if (r["apiStatus"] == "1"){
                    alert("设置成功");
                }else{
                    alert(r["errorMsg"]);
                }

            }else{
                alert("服务器异常");
            }
        }
    };
    xmlhttp.send(JSON.stringify(data));
}


function controlTopDiv1(whichWindow, mySwitch, text){
    var topDiv = document.getElementById(whichWindow);
    if (mySwitch == "open") {
        //设置页面抬头
        topDiv.getElementsByTagName("label")[0].innerText = text;
        //topDiv.getElementById("topDivTitle").innerText = text;
        //显示页面
        topDiv.style.marginTop = document.documentElement.scrollTop + "px";
        topDiv.style.display = "block";
        //设置分页方法 empListGO
        var empListGO = document.getElementById("empListGO");
        empListGO.onclick = function () {
            var cId = document.getElementById("empList").getAttribute("corpid");
            var toPage = document.getElementById("empListC").value;
            empListTable(cId, toPage);
        };

        //设置分页方法 empListP
        var empListP = document.getElementById("empListP");
        empListP.onclick = function (){
            var empListC = parseInt(document.getElementById("empListC").value);
            if(empListC == 1){
                return;
            }
            empListC = empListC - 1;
            var cId = document.getElementById("empList").getAttribute("corpid");
            empListTable(cId, empListC);
        };

        //设置分页方法 empListN

        var empListN = document.getElementById("empListN");
        empListN.onclick = function (){
            var empListPageCount = parseInt(document.getElementById("empListPageCount").innerText);
            var empListC = parseInt(document.getElementById("empListC").value);
            if(empListC == empListPageCount){
                return;
            }
            empListC = empListC + 1;
            var cId = document.getElementById("empList").getAttribute("corpid");
            empListTable(cId, empListC);
        }
    }
    if (mySwitch == "close") topDiv.style.display = "none";
}

//添加公司
function saveCorp(){
    var env = document.getElementById("env");
    var envText = env.options[env.selectedIndex].value;
    var corpName = document.getElementById("corpName").value;
    var corpCity = document.getElementById("corpCity").value;
    var corpMail = document.getElementById("corpMail").value;
    var corpType = document.getElementById("corpType");
    var corpTypeText = corpType.options[corpType.selectedIndex].value;
    var corpLogo = document.getElementById("corpLogo").value;
    var corpAddress = document.getElementById("corpAddress").value;
    var corpMobile = document.getElementById("corpMobile").value;
    var point_name = document.getElementById("point_name").value;
    var rmb_point_ratio = document.getElementById("rmb_point_ratio").value;

    var data = {
        "action": "corpCreate",
        "env": envText,
        "corpName": corpName,
        "corpCity": corpCity,
        "corpMail": corpMail,
        "corpType": corpTypeText,
        "corpLogo": corpLogo,
        "corpAddress": corpAddress,
        "corpMobile": corpMobile,
        "point_name": point_name,
        "rmb_point_ratio": rmb_point_ratio
    };

    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open("POST", "/efuli/corpMng", true);
    xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState==4){
            if (xmlhttp.status==200){
                var r = JSON.parse(xmlhttp.response);
                if (r["apiStatus"] == "1"){
                    controlTopDiv('add&edit', 'close', '');
                    initializeData();
                }else{
                    alert(r["errorMsg"]);
                }

            }else{
                alert("服务器异常");
            }
        }
    };
    xmlhttp.send(JSON.stringify(data));
}

//员工列表
function empListTable(corpid, toPage){
    //empList
    controlTopDiv1('empList', 'open', '员工列表');
    var realName = document.getElementById("realName").value;
    var env = document.getElementById("env");
    var envText = env.options[env.selectedIndex].value;
    document.getElementById("empList").setAttribute("corpid", corpid);
    var empTable = document.getElementById("empTable").getElementsByTagName("tbody")[0];
    var firstTr = empTable.getElementsByTagName("tr")[0];
    empTable.innerText = "";
    empTable.appendChild(firstTr);

    var empListPageCount = document.getElementById("empListPageCount");
    var empListC = document.getElementById("empListC");
    //获取分页总数
    var data1 = {
        "action": "getCorpMemCountPage",
        "env": envText,
        "corp_id": corpid,
        "real_name": realName
    };
    var xmlhttp1 = new XMLHttpRequest();
    xmlhttp1.open("POST", "/efuli/corpMng", true);
    xmlhttp1.onreadystatechange = function(){
        if (xmlhttp1.readyState==4){
            var r = "";
            if (xmlhttp1.status==200){
                r = JSON.parse(xmlhttp1.response);
                if (r["apiStatus"] == "1"){
                    empListPageCount.innerText = "";
                    empListPageCount.innerText = r["result"];
                    empListC.value = toPage;
                }
            }else{
                alert(r["errorMsg"]);
            }
        }
    };
    xmlhttp1.send(JSON.stringify(data1));

    //获取员工列表
    var data = {
        "action": "getCorpMemList",
        "env": envText,
        "toPage": toPage,
        "corp_id": corpid,
        "real_name": realName
    };
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open("POST", "/efuli/corpMng", true);
    xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState==4){
            if (xmlhttp.status==200){
                var r = JSON.parse(xmlhttp.response);
                if (r["apiStatus"] == "1"){
                    var list = r["result"];
                    for(var i = 0; i < list.length; i++){
                        var tr = document.createElement("tr");

                        //名字
                        var td1 = document.createElement("td");
                        td1.innerText = list[i]["real_name"];
                        tr.appendChild(td1);

                        //工号
                        var td2 = document.createElement("td");
                        td2.innerText = list[i]["job_no"];
                        tr.appendChild(td2);

                        //余额
                        var td3 = document.createElement("td");
                        td3.innerText = list[i]["account_balance"];
                        tr.appendChild(td3);

                        //阿里id
                        var td4 = document.createElement("td");
                        td4.innerText = list[i]["ali_user_id"];
                        tr.appendChild(td4);

                        //员工充值
                        var td5 = document.createElement("td");
                        var myInput5 = document.createElement("input");
                        myInput5.setAttribute("class", "forTD");
                        myInput5.style.backgroundColor = "transparent";
                        myInput5.placeholder = "输入金额";
                        myInput5.style.width = "80px";
                        var mySpan2 = document.createElement("span");
                        mySpan2.innerText = "充";
                        mySpan2.style.width = "20px";
                        mySpan2.style.cursor = "pointer";
                        mySpan2.onclick = function(){memberRecharge(this.parentNode.parentNode)};
                        td5.appendChild(myInput5);
                        td5.appendChild(mySpan2);
                        tr.appendChild(td5);

                        empTable.appendChild(tr);
                    }
                }else{
                    alert(r["errorMsg"]);
                }
            }
        }
    };
    xmlhttp.send(JSON.stringify(data));

}

//添加员工弹出层
function emp(corpid){
    controlTopDiv('empEdit', 'open', '添加员工');
    document.getElementById("empEdit").setAttribute("corpid", corpid);
}

//企业充值
function corpRecharge(corpId, td){
    var env = document.getElementById("env");
    var envText = env.options[env.selectedIndex].value;
    var amount = td.getElementsByTagName("input")[0].value.trim();
    var data = {
        "action": "corpRecharge",
        "env": envText,
        "corp_id": corpId,
        "amount": amount
    };
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open("POST", "/efuli/corpMng", true);
    xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState==4){
            if (xmlhttp.status==200){
                var r = JSON.parse(xmlhttp.response);
                if (r["apiStatus"] == "1"){
                    alert("充好了");
                }else{
                    alert(r["errorMsg"]);
                }

            }else{
                alert("服务器异常");
            }
        }
    };
    xmlhttp.send(JSON.stringify(data));
}

//员工充值
function memberRecharge(tr){
    var env = document.getElementById("env");
    var envText = env.options[env.selectedIndex].value;
    var corpId = document.getElementById("empList").getAttribute("corpid");
    var job_no = tr.getElementsByTagName("td")[1].innerText.trim();
    var rechargeNumber = tr.getElementsByTagName("td")[4].getElementsByTagName("input")[0].value;
    var page_no = document.getElementById("empListC").value;
    var data = {
        "action": "recharge",
        "env": envText,
        "corp_id": corpId,
        "job_no": job_no,
        "rechargeNumber": rechargeNumber
    };

    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open("POST", "/efuli/corpMng", true);
    xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState==4){
            if (xmlhttp.status==200){
                var r = JSON.parse(xmlhttp.response);
                if (r["apiStatus"] == "1"){
                    controlTopDiv("empList", "close");
                    empListTable(corpId, page_no);
                }else{
                    alert(r["errorMsg"]);
                }

            }else{
                alert("服务器异常");
            }
        }
    };
    xmlhttp.send(JSON.stringify(data));

}

//添加员工
function saveEmp(){
    var env = document.getElementById("env");
    var envText = env.options[env.selectedIndex].value;

    var cert_type = document.getElementById("cert_type");
    var cert_typeText = cert_type.options[cert_type.selectedIndex].value;
    var cert_no = document.getElementById("cert_no").value;
    var name = document.getElementById("name").value;
    var cardno = document.getElementById("cardno").value;
    var mobile = document.getElementById("mobile").value;
    var department = document.getElementById("department").value;

    var corpid = document.getElementById("empEdit").getAttribute("corpid");

    var member = {
        "cert_type": cert_typeText,
        "cert_no": cert_no,
        "name": name,
        "cardno": cardno,
        "mobile": mobile,
        "department": department};
    var members = [member];
    var data = {
        "action": "empCreate",
        "env": envText,
        "corpCode": corpid,
        "members": members
    };

    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open("POST", "/efuli/corpMng", true);
    xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState==4){
            if (xmlhttp.status==200){
                var r = JSON.parse(xmlhttp.response);
                if (r["apiStatus"] == "1"){
                    controlTopDiv('empEdit', 'close', '');
                    initializeData();
                }else{
                    alert(r["errorMsg"]);
                }

            }else{
                alert("服务器异常");
            }
        }
    };
    xmlhttp.send(JSON.stringify(data));
}